<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Peach-Printer 表格编辑器 3.0</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="Keywords" content="web打印,peach-printer,前端自动分页打印" />
    <meta name="Description" content="Peach-Printer是一款优秀的Web打印解决方案，支持无需预览直接打印、表格分页汇总、调用简单，兼容主流浏览器及IE。" />
    <link rel="shortcut icon" href="logo.png">
    <link rel="stylesheet" href="core/peach-ui.css">
    <link rel="stylesheet" href="core/peach-designer.css">
    <link rel="stylesheet" href="icon/iconfont.css">
    <script src="core/peach-designer-min.js"></script>
    <script src="core/peach-jprinter-min.js"></script>
</head>

<body class="flex-col">
    <script>
        const opt = {
            saveAction: function (data) {
                console.log("保存数据", JSON.stringify(data))
                //这里可以将data发送到服务器保存
                alert("此处回调保存代码")
            },
            previewAction: function (template, maindata, listdata) {
                //设计器打印回调
                PeachPrinter.preview(template, maindata, listdata)
            },
            printAction: function (template, maindata, listdata) {
                //设计器打印回调
                const licence = {
                    code: "注册码",
                    name: "注册名",
                };
                //使用局域网打印机服务
                //PeachPrinter.setService("ws://192.168.0.141:9623/ws")
                PeachPrinter.print(template, maindata, listdata, licence, (result) => {
                    console.info(result)
                })
                // PeachPrinter.savePdf(template, maindata, listdata, licence, (result) => {
                //     console.info(result)
                // })
                // (async () => {
                //     const htmlstr = await loadHtmlData("data/table.html")
                //     PeachPrinter.previeHtml(htmlstr, {
                //         marginBottom: 10,
                //         pageNumber: {
                //             position: "top-right",
                //             content: "{PageNO}/{PageCount}",
                //             size: 14
                //         }
                //     }, licence)
                // })();
            }
        }
        window.onload = async () => {
            const datasource = await loadData("data/data1.json")
            //加载文档
            const template = await loadData("data/template1.json")
            PeachDesigner.loadTemplate(opt, template, datasource.maindata, datasource.listdata);
            //创建新文档
            //PeachDesigner.newTemplate(opt,datasource.maindata, datasource.listdata)
        }

        async function loadData(url) {
            const response = await fetch(url, {
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            // 检查响应状态
            if (!response.ok) {
                throw new Error(`HTTP错误! 状态码: ${response.status}`);
            }
            // 解析JSON响应
            return await response.json();
        }

        async function loadHtmlData(url) {
            const response = await fetch(url);
            // 检查响应状态
            if (!response.ok) {
                throw new Error(`HTTP错误! 状态码: ${response.status}`);
            }
            // 解析JSON响应
            return await response.text();
        }
    </script>
</body>

</html>